package carrier.service;


import carrier.client.UserClient;
import carrier.mapper.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.entity.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CarrierService {
    @Resource
    CarrierMapper carrierMapper;
    @Resource
    UserClient userClient;
    @Resource
    TruckMapper truckMapper;
    @Resource
    CarrierDriverMapper carrierDriverMapper;


    public List<Carrier> getInfo(String name) {
        QueryWrapper wrapper = new QueryWrapper();
        if(name!=null && name!="") wrapper.like("name", name);
        List<Carrier> cl = carrierMapper.selectList(wrapper);
        cl.stream().map(carrier -> moreDetail(carrier)).collect(Collectors.toList());
        return cl;
    }

    public Carrier findCarrier(Long id) {
        Carrier c = carrierMapper.selectById(id);
        c = moreDetail(c);
        return c;
    }

    private Carrier moreDetail(Carrier c){
        User user = userClient.selectById(c.getUserId());
        c.setUser(user);
        List<User> driverList = userClient.selectDriver(0,c.getId());
        c.setDrivers(driverList);
        List<Truck> truckList = truckMapper.selectList(new QueryWrapper<Truck>().eq("carrier_id", c.getId()));
        c.setTrucks(truckList);
        return c;
    }

    public List<User> findFreeDrivers(Long id) {
        List<User> dl = userClient.selectDriver(0,id);
        dl.forEach(d->{
            d.setPassword("1");
            d.setEmail("1");
        });
        return dl;
    }

    public List<Truck> findFreeTrucks(Long id) {
        return truckMapper.selectList(new QueryWrapper<Truck>().eq("carrier_id", id).eq("state",0));
    }

    public int update(Carrier c) {
        return carrierMapper.updateById(c);
    }

    public void addTruck(Truck truck) {
        truckMapper.insert(truck);
    }


    public void addDriver(Long carrierId, String userName, String realName, String password, int id, String phone, String email) {
        User u = userClient.registerDriver(new User(userName,realName,password,phone,email,id));
        carrierDriverMapper.insert(new CarrierDriver(carrierId,u.getId()));
    }

    public void updateTruck(Truck truck) {
        truckMapper.updateById(truck);
    }

    public Truck selectTruck(Long truckId) {
        return truckMapper.selectById(truckId);
    }

    public Float getUnitPrice(Long carrierId) {
        Carrier c = carrierMapper.selectById(carrierId);
        return c.getUnitPrice();
    }

    public void updateDriver(Integer state, Long carrierId, Long driverId) {
        CarrierDriver cd = new CarrierDriver(carrierId,driverId);
        cd.setState(state);
        carrierDriverMapper.update(cd,new UpdateWrapper<CarrierDriver>().eq("driver_id",driverId));
    }

    public Carrier findCarrierByUser(Long id) {
        return carrierMapper.selectOne(new QueryWrapper<Carrier>().eq("user_id",id));
    }
}
